package com.bdqn.ch02.sl;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
public class DOM4JParse {
    /*
    显示所有信息
     */
    public void showInfo(){
        Document doc=null;
            try {
                    //加载DOM列
                SAXReader saxResult= new SAXReader();
                doc =saxResult.read(new File("src/dao/PhoneInfo.xml"));
            } catch (DocumentException e) {
              e.fillInStackTrace();
            }
            //获取XML根节点
            Element root=doc.getRootElement();
            //遍历所有的Brand标签
            for (Iterator itBrand = root.elementIterator(); itBrand.hasNext();){
                Element brandEle=(Element)itBrand.next();
                //输出Brand标签的name属性的属性值
                System.out.println("品牌:"+brandEle.attributeValue("name"));
                //遍历Type标签
                for (Iterator itType = brandEle.elementIterator(); itType.hasNext();){
                    Element typeEle=(Element) itType.next();
                    //输出标签的name属性
                    System.out.println("\t型号:"+typeEle.attributeValue("name"));
                }
            }
    }
    public static void main(String[] args) {
        DOM4JParse dom4JParse=new DOM4JParse();
        dom4JParse.showInfo();
    }
}


